/**
 * 家庭成员
 * @param {Object} windowObj  窗体操作对象
 * @param {Object} fromId  个人住户表单ID
 */
function Individual(windowObj,fromId){

	this.windowObj = windowObj;

	this.formId = fromId;   //当前个人住户表单ID
	this.formObj = document.getElementById(fromId); //当前个人住户表单

	var oldCertificateNo = null;
	var oldCertificateType = null;
	var oldHouseId = null;
	var isTrue = null;

	/**
	 * 初始化方法
	 */
	this.init = function(){
		setCertificate(); //设置证件类型、证件号码的值
	}

	/**
	 * 通过证件获取、并设置个人住户信息
	 */
	this.setInfoByCertificate = function(){
		var certificateNo = document.forms(this.formId).certificateNo.value;
		var certificateType = document.forms(this.formId).certificateType.value;
		var houseId = document.forms(this.formId).houseNumber.value;
		if(checkAllowSubmit(certificateNo,certificateType,houseId)){
			//判断当前证件个人信息能否作家庭成员添加
			return isAllowLoad(certificateNo,certificateType);
		}else{
			return true;
		}
	}

	/**********私有方法***************/
	function checkAllowSubmit(certificateNo,certificateType,houseId){
		//房间ID 不到后台拿数据
		if(houseId == null || houseId == "") return false;
		//证件号码为空 不到后台拿数据
		if(certificateNo == null || certificateNo == "") return false;
		//证件类型、号码、房号同上次的相同，不到后台拿数据
		if(certificateNo == oldCertificateNo && certificateType == oldCertificateType&&houseId == oldHouseId) return false;
		return true;
	}
	/**
	 * 验证当前证件信息能否作为家庭成员添加
	 * @param {Object} certificateNo  证件号码
	 * @param {Object} certificateType 证件类型
	 */
	function isAllowLoad(certificateNo,certificateType){
		var url = PROJECT_NAME_PATH_MYCE + "/customer_memberAction.do?method=validateMemberAllowLoad";
		var parameters = "certificateType=" + certificateType + "&certificateNo=" + certificateNo;
			parameters += "&participantId=" + document.forms(this.formId).participantId.value;
			parameters += "&houseId=" + document.forms(this.formId).houseId.value;
		sendAjaxRequest(url,parameters,function(result){
			var obj = result.responseText;
			//可以导入
			if(obj == 'true'){
				getIndividualInfo(certificateNo,certificateType);
				isTrue = true;
			}else if(obj == 'owner'){
				Alert("此用户已经登记为该房间的业主了，请重新输入!");
				setCertificateNull();
				isTrue = false;
			}//该住户已经是租户,不能导入
			else if(obj == 'lessee'){
				Alert("此用户已经登记为该房间的租户了,请重新输入!");
				setCertificateNull();
				isTrue = false;
			}//该住户已经是家庭成员了，不可导入
			else if(obj == 'member'){
				Alert("此用户已经登记为该房间的家庭成员了,请重新输入!");
				setCertificateNull();
				isTrue = false;
			}
		});
		return isTrue;
	}
	/**
	 * 从后台获取个人信息
	 * @param {Object} certificateNo
	 * @param {Object} certificateType
	 */
	function getIndividualInfo(certificateNo,certificateType){
		var url = PROJECT_NAME_PATH_MYCE + "/customer_memberAction.do?method=queryParticipant";
		var parameter = "certificateType=" + certificateType + "&certificateNo=" + certificateNo;
		sendAjaxRequest(url,parameter,setIndividualInfo);
	}



	/**
	 * 根据提供的个人信息，设置表单信息
	 * @param {Object} IndividualInfo
	 */
	function setIndividualInfo(individualInfo){
		if(obj == 'false') return;

		if(individualInfo.responseText == 'false') return;

		var flag = confimImport();
		if(!flag) return;
		var obj=eval('(' + individualInfo.responseText + ')');
		setCertificate(); //重新初始化证件类型、证件号码
		document.forms(this.formId).participantId.value = obj.participantId //参与人ID
		document.forms(this.formId).participantName.value = obj.participantName; //参与人姓名
		document.forms(this.formId).appellative.value = obj.appellative; //参与人称呼
		document.forms(this.formId).telephone.value = obj.telephone; //参与人电话
		setSexValue(document.forms(this.formId).sexId,obj.sex);

		document.forms(this.formId).birthday.value = obj.birthDate; //出生日期
		document.forms(this.formId).homePlace.value = obj.homeplace;//籍贯
		document.forms(this.formId).registerPlace.value = obj.registerPlace; //户口所在地
		document.forms(this.formId).profession.value = obj.profession; //职业
		document.forms(this.formId).remark.value = obj.remark; //备注

		//处理兴趣爱好

		//将已选择中所有的选项移到左边
		var selectedOptions = $('selectedInterest').options;
		var length = selectedOptions.length;
		for(i = 0 ; i  < length; i++){
			$('unSelectInterest').appendChild(selectedOptions[0]);
		}

		var opt = $('unSelectInterest').options;//所有兴趣爱好
		var interestArray = new Array();
		for(i = 0 ; i < obj.interest.length; i++){
			for(j = 0; j < opt.length; j++){
				if(obj.interest[i] == opt[j].value){
					interestArray[interestArray.length] = opt[j].value;
					$('selectedInterest').appendChild(opt[j]);
					j--;
				}
			}
		}

		$("interest").value = interestArray.join(",");
	}


	function confimImport(){
		if(confirm("你确定要导入相关记录吗？")){
			return true;
		}
		setCertificateNull();
		return false;
	}

	/**
	 * 设置证件类型，证件号码,房间ID的值
	 */
	function setCertificate(){
		oldCertificateNo = document.forms(this.formId).certificateNo.value;
		oldCertificateType = document.forms(this.formId).certificateType.value;
		oldHouseId = document.forms(this.formId).houseNumber.value;
	}
	/**
	*将证件类型至空
	*/
	function setCertificateNull(){
		document.forms(this.formId).certificateNo.value = "";
		document.forms(this.formId).certificateType.selectedIndex = 0;
	}
}


/***************公共方法,类*******************/

/**
* 设置下拉框选中的值
*/
function setSelectValue(selectObj,selectedValue){
	var options = selectObj.options;
	for(i = 0; i < options.length; i++){
		if(options[i].value == selectedValue){
			selectObj.selectedIndex = i;
			return;
		}
	}
}
/**
*  设置性别选中
*/
function setSexValue(sexObj,showValue){
	for(i = 0; i < sexObj.length; i++){
		if(sexObj[i].value == showValue){
			sexObj[i].checked=true;
			return;
		}
	}
}
/**
 * 窗体操作对象
 * @param {Object} saveAndUpdateId  保存并新增按钮ID
 * @param {Object} saveButtonId  保存按钮ID
 * @param {Object} cancelButtonId  取消按钮ID
 */
function WindowObj(saveAndUpdateId,saveButtonId,cancelButtonId){

	//相关按钮
	saveAndNewButton = document.getElementById(saveAndUpdateId);
	saveButton = document.getElementById(saveButtonId);
	cancelButton = document.getElementById(cancelButtonId);

	/**
	 * 设置按钮是否可用
	 * @param {Object} flag  true:可用  false:不可用
	 */
	this.setButtonUserable = function(flag){
		if(saveAndNewButton)
			saveAndNewButton.disabled = flag;
		if(saveButton)
			saveButton.disabled = flag;
		if(cancelButton)
			cancelButton.disabled = flag;
	}

}

/**
*  兴趣爱好操作对象
*/
function Interest(rightSelectId,leftSelectId){

	var rightSelectObj = document.getElementById(rightSelectId);
	var leftSelectObj = document.getElementById(leftSelectId);

	//右移操作
	this.moveToRight = function(thiz){
		var opt = leftSelectObj.options[leftSelectObj.selectedIndex];
	    rightSelectObj.appendChild(opt);
	    setInterest();
	}
	//左移操作
	this.moveToLeft = function(){
 		if (rightSelectObj.selectedIndex<0) return;
        var opt = rightSelectObj.options[rightSelectObj.selectedIndex];
        leftSelectObj.appendChild(opt);
        setInterest();
	}

	//设置选择的兴趣爱好
	function setInterest(){
		var objOptions = rightSelectObj.options;
		var selectVar = "";
		for(i = 0; i < objOptions.length; i++){
			selectVar += objOptions[i].value + ",";
		}
		//selectVar += objOptions[objOptions.length-1].value;

		document.getElementById('interest').value = selectVar;
	}
}

/**
 * 发送ajax请求，简单封装下
 * @param {Object} url  请求URL
 * @param {Object} parameters 请求参数
 * @param {Object} callBack  响应回调
 */
function sendAjaxRequest(url,parameters,callBack){
	 new Ajax.Request(url,
            {method: 'post', parameters: parameters, onComplete: callBack}
            );
}

